window.onload = function() {
    mui.init({
        swipeBack: true //启用右滑关闭功能
    });
}
$(function() {
    $('body').on('tap', 'a', function() {
        mui.openWindow({
            url: this.href,
        })
    })
    $('#cart').on('tap', function() {
        if (localStorage.getItem('username')) {
            $(this).attr('href', 'shoppingcar.html')
        } else {
            $(this).attr('href', 'login.html')
        }
    })
    $('#my').on('tap', function() {
        if (localStorage.getItem('username')) {
            $(this).attr('href', 'user.html')
        } else {
            $(this).attr('href', 'login.html')
        }
    })
    $.ajax({
        url: '/cart/queryCart',
        data: {
            page: 1,
            pageSize: 999,
        },
        success: function(res) {
            if (localStorage.getItem('username') && localStorage.getItem('password')) {
                $('.mui-badge').css('display', 'block')
                if (res.length == 0) {
                    $('.mui-badge').css('display', 'none')
                }
                $('.mui-badge').html(res.length)
            }

        }
    })


})

function queryStr() {
    var href = location.search
    if (!href) return false
    var obj = {}
    var arr = []
    var keys = href.slice(1)

    if (keys.includes('&')) {
        var a = keys.split('&')
        for (var k in a) {
            var arr = a[k].split('=')
            obj[arr[0]] = arr[1]
        }
    } else {
        var arr = keys.split('=')
        obj[arr[0]] = arr[1]
    }
    return obj
}